package forms;

import helpers.ValidateHelper;

import java.net.URL;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import data.EmailData;

import entities.Email;


public class EmailForm {
	public static TreeMap<String, Object> checkValid(HttpServletRequest request, Email email) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> checkValid = new TreeMap<String, String>();
		
		String emailAddress = (String) request.getParameter("emailAddress");
		String status = (String) request.getParameter("status");
		
		if (emailAddress == null || emailAddress.equals("")) {
			checkValid.put("email", " Email is not empty.");
			email.setEmail("");
		} else if (!ValidateHelper.isEmail(emailAddress)) {
			checkValid.put("email", " Email is not valid.");
			email.setEmail(emailAddress);
		} else if (EmailData.checkExists(emailAddress)) {
			checkValid.put("email", "Email has been exists.");
			email.setEmail(emailAddress);
		} else {
			email.setEmail(emailAddress);
		}
		
		
		if (status == null || status.isEmpty()) {
			checkValid.put("status", "Status is not emtpy");
		} else {
			try {
				email.setStatus(Integer.parseInt(status));
			} catch (Exception e) {
				checkValid.put("status", "Status must be a number!");
			}
		}
		
		form.put("checkValid", checkValid);
		form.put("email", email);
		return form;
	}
	
}
